from time import time
import demjson
from flask import Flask, render_template, url_for, request, redirect, send_from_directory
import os
import socket
app = Flask(__name__)
taskinfo = {
        "type": "http",
        "charset": "utf-8",
        "allow_redirects": "0",
        "contain_list": ["hello", "world"],
        "exclude_list": ["cfuid", "acw"],
        "headers": {"host": "xss.app"},
        "path": "/login",
        "method": "GET",
        "thread": 3,
        "timeout": 3,
        "ipfile": "ip.txt",
        "task_id": "dev-test-1",
        "force":1
    }
print(demjson.encode(taskinfo))
from server import server
s = server()
def get_my_ip():
    '''获取当前IP'''
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    return myaddr

@app.route('/')
def hello_world():
    return "hello world"

@app.route('/get_all_detector')
def get_all_detector():
    return demjson.encode(s.detector_list)

@app.route('/submit_report',methods=['POST'])
def submit_report():
    try:
        report_info = demjson.decode(request.form['report_info'])
        report_info['ip'] = request.remote_addr  # 设置IP
        report_info['report_time'] = time()  # 设置报告时间
        s.receive_report(report_info)
        return demjson.encode({
            "status":"success",
            "text":"report success......"
        })
    except Exception as e:
        return demjson.encode({
            "status":"exception",
            "reason":e
        })
@app.route("/get_all_ips")
def get_all_ips():
    try:
        res = os.listdir("static/data/ips/")
        return demjson.encode({
            "status":"success",
            "data":res
        })
    except Exception as e:
        return demjson.encode({
            "status":"failed",
            "reason":e
        })


@app.route("/get_success_report")
def get_success_report():
    try:
        res = os.listdir("static/data/success/")
        return demjson.encode({
            "status": "success",
            "data": res
        })
    except Exception as e:
        return demjson.encode({
            "status": "failed",
            "reason": e
        })
@app.route('/submit_task',methods=['POST'])
def submit_task():
    try:
        task_info = request.form['task_info']
        res = s.send_grab_all(task_info)
        print(res)
        return demjson.encode(res)
    except Exception as e:
        return demjson.encode({
            "status":"failed",
            "reason":e
        })

@app.route('/report_success', methods=['POST'])
def report_success():
    try:
        success_info = demjson.decode(request.form['success_info'])
        res = s.success_report(success_info)
        if res:
            return demjson.encode({
                "status": "success"
            })
        else:
            return demjson.encode({
                "status": "failed"
            })
    except Exception as e:
        return demjson.encode({
            "status": "failed",
            "reason": e
        })
@app.route("/stop_all")
def stop_all():
    try:
        s.stop_all()
        return demjson.encode({
            "status": "success",
            "reason": "submit stop command success."
        })
    except Exception as e:
        return demjson.encode({
            "status": "failed",
            "reason": e
        })
@app.route("/get_myip")
def get_myip():
    return request.remote_addr
if __name__ == '__main__':
    app.debug = False
    app.run(host="0.0.0.0", port=8089)
